/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dabarobjects.mobile.database.types;

import java.util.Vector;

/**
 * Complex Entries are stored as entries
 * Store > MadexRowItem
 * Get > List<MadexRowItem>
 * @author DabarObjects Solutions Limited (www.dabarobjects.com)
 */
public class ComplexObjectNotationParser extends AbstractMadexObjectNotationParser {

    public String getObjectTypeKey() {
        return "ct";
    }
    //private static String COMPLEX = "(PK[AA]CT[]TS[Y#2010-M#5-D#11-H#3-I#18-S#30-]ID[indexLevel]DT[data]),()";

    //&5,
    //called while retrieving object
    public Object parseObject(String cmplxnotation) {

        return splitItems(cmplxnotation, "&", ",");

    }
 
    //Called during while data is being stored

    public String notateObject(Object obj) {
        return ""+obj;
    }


    /**
     * Breaks (expr1)(expr2) into expr1 and expr2
     * @param cmplxnotation
     */
    public Vector splitItems(String cmplxnotation, String start, String end) {

        Vector vec = new Vector();
        int foundCount = 0;
        int len = cmplxnotation.length();
        StringBuffer cmplxDataBuffer = new StringBuffer();
        boolean startAppend = false;
        for (int i = 0; i < len; i++) {
            String it = cmplxnotation.substring(i, i + 1);
            if (it.equalsIgnoreCase(start)) {
                startAppend = true;
                continue;
            }
            if (it.equalsIgnoreCase(end)) {
                startAppend = false;
                foundCount++;
                vec.addElement(cmplxDataBuffer.toString());

                cmplxDataBuffer = new StringBuffer();
                continue;
            }

            if (startAppend) {
                cmplxDataBuffer.append(it);
            }

        }
        return vec;
    }

    public int countItems(String cmplxnotation, String start, String end) {

        int foundCount = 0;
        int len = cmplxnotation.length();
        for (int i = 0; i < len; i++) {
            String it = cmplxnotation.substring(i, i + 1);
            if (it.equalsIgnoreCase(start)) {
                continue;
            }
            if (it.equalsIgnoreCase(end)) {

                foundCount++;

                continue;
            }

        }
        return foundCount;

    }


}
